package dispatch

import (
	"engine/log"
	"gitee.com/ameise84/e3dds"
)

var _gCallBack map[string]*dispatcher

func init() {
	_gCallBack = map[string]*dispatcher{}
}

type dispatcher struct {
	eventsFunc map[uint32][]EventFunc
}

func (d *dispatcher) register(event uint32, callback EventFunc) {
	d.eventsFunc[event] = append(d.eventsFunc[event], callback)
}

func (d *dispatcher) trigger(actor e3dds.Behavior, event uint32, ctx ...any) {
	if fs, ok := d.eventsFunc[event]; ok {
		for _, cb := range fs {
			cb(actor, ctx...)
		}
	} else {
		log.GetLogger().Warn("[dispatch] the event not found in dispatcher").Println()
	}
}
